package sjq.com.time;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import sjq.com.time.util.CommandAdapter;
import sjq.com.time.util.DataBase;
import sjq.com.time.util.PickerScrollView;
import sjq.com.time.util.ViewHolder;

/**
 * Created by chengengsen on 2016/7/29.
 */
public class SecondActivity extends AppCompatActivity{

    private Context context;

    PickerScrollView picker_hour,picker_mimute;

    private String[] date = {"日","一","二","三","四","五","六"};

    private RecyclerView recyclerView;

    private List<Pickers> hourList,mimuteList;
    private List<sjq.com.time.Date> dateList;
    private List<sjq.com.time.Date> choseDate  = new ArrayList<>();;

    private String hour, mimute;

    private Button btn;

    private String type;

    DataBase dataBase;

    java.util.Date dateTime;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        context = this;

         dataBase = new DataBase(this);

        dateTime = new java.util.Date();


        picker_hour = (PickerScrollView) findViewById(R.id.picker_hour);
        picker_mimute = (PickerScrollView) findViewById(R.id.picker_mimute);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
        btn = (Button) findViewById(R.id.btn);

        type = getIntent().getStringExtra("type");
        initData();
        initRecyclerView();

        picker_hour.setOnSelectListener(new PickerScrollView.onSelectListener() {
            @Override
            public void onSelect(Pickers pickers) {
                hour = pickers.getShowConetnt();
            }
        });

        picker_mimute.setOnSelectListener(new PickerScrollView.onSelectListener() {
            @Override
            public void onSelect(Pickers pickers) {
                mimute = pickers.getShowConetnt();
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(hour == null){
                    hour = ""+dateTime.getHours();
                }
                if(mimute == null){
                    mimute = ""+dateTime.getMinutes();
                }



                dataBase.open();
                long id = dataBase.insertContact(hour,mimute);
                dataBase.close();

                Intent intent = new Intent(context,OneReceive.class);
                PendingIntent pendingIntent = PendingIntent.getBroadcast(context, (int)id, intent, 0);

                Calendar calendar = Calendar.getInstance();
                calendar.setTimeInMillis(System.currentTimeMillis());

                calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour));
                calendar.set(Calendar.MINUTE, Integer.parseInt(mimute));
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                //获取闹钟管理器
                AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
                //设置闹钟
                alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);
                Toast.makeText(context, "设置闹钟的时间为："+hour+":"+mimute, Toast.LENGTH_SHORT).show();

                finish();
            }
        });

    }


    /**
     * 初始化数据
     */
    private void initData() {
        hourList = new ArrayList<>();
        mimuteList = new ArrayList<>();
        dateList = new ArrayList<>();

        for(int i = 0; i <= 23; i++){
            hourList.add(new Pickers(""+i,""+i));
        }

        for(int i = 0; i <= 59; i++){
            mimuteList.add(new Pickers(""+i,""+i));
        }

        for(int i = 0; i < date.length; i++){
            sjq.com.time.Date d = new sjq.com.time.Date();
            d.setDate(date[i]);
            dateList.add(d);
        }


        // 设置数据，默认选择第一条
        picker_hour.setData(hourList);
        picker_mimute.setData(mimuteList);

        if(type.equals("1")){
            picker_hour.setSelected(dateTime.getHours());
            picker_mimute.setSelected(dateTime.getMinutes());
        } else{

            SimpleDateFormat dateFormat = new SimpleDateFormat();
            dateFormat.applyPattern("HH:mm");
            java.util.Date newDate = null;
            try {
                newDate = dateFormat.parse(getIntent().getStringExtra("hour") + ":" + getIntent().getStringExtra("mimute"));
            }catch (ParseException e){
                e.printStackTrace();
            }

            picker_hour.setSelected(newDate.getHours());
            picker_mimute.setSelected(newDate.getMinutes());

        }
    }


    private void initRecyclerView(){

        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false));//设置布局管理器
        recyclerView.setHasFixedSize(true); //加速

        recyclerView.setAdapter(new CommandAdapter<Date>(this, R.layout.item_date, dateList) {
            @Override
            protected void convert(final ViewHolder holder, final  sjq.com.time.Date d, int position) {

                holder.setText(R.id.item_tv, dateList.get(position).getDate());

                holder.setOncClickListener(R.id.item_tv, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(!d.isChoose()){
                            holder.setBackgroundDrawable(R.id.item_tv, getResources().getDrawable(R.drawable.shape_bg_circular2));
                            holder.setTextColor(R.id.item_tv, Color.parseColor("#ffffff"));
                            d.setChoose(true);
                            choseDate.add(d);
                        }else{
                            holder.setBackgroundDrawable(R.id.item_tv, getResources().getDrawable(R.drawable.shape_bg_circular));
                            holder.setTextColor(R.id.item_tv, Color.parseColor("#666666"));
                            d.setChoose(false);
                        }
                    }
                });

            }
        });

    }

}
